package org.example.tms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.tms.config.AjaxResult;
import org.example.tms.mapper.ClazzMapper;
import org.example.tms.mapper.StudentMapper;
import org.example.tms.pojo.Clazz;
import org.example.tms.pojo.PageBean;
import org.example.tms.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzMapper clazzMapper;

    @GetMapping
    public AjaxResult getStudent(String name, Integer degree,Integer clazzId,
                              @RequestParam(defaultValue = "1",name="page") Integer pageNum,
                              @RequestParam(defaultValue = "1") Integer pageSize) {
        Page<Student> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(name!=null&& !name.isEmpty(),"name",name);
        queryWrapper.eq(degree!=null,"degree",degree);
        queryWrapper.eq(clazzId!=null,"clazz_id",clazzId);
        queryWrapper.orderByDesc("id");
        studentMapper.selectPage(page,queryWrapper);

        for(Student student : page.getRecords()){
            Clazz clazz = clazzMapper.selectById(student.getClazzId());
            if(clazz!=null){
                student.setClazzName(clazz.getName());
            }

        }

        PageBean pageBean = new PageBean();
        pageBean.setRows(page.getRecords());
        pageBean.setTotal(page.getTotal());

        return AjaxResult.success(pageBean);

    }
    @DeleteMapping("/{ids}")
    public AjaxResult deleteStudent(@PathVariable List<Integer> ids){
        for(Integer id:ids){
            studentMapper.deleteById(id);
        }
        return AjaxResult.success();

    }
    @PostMapping
    public AjaxResult addStudent(@RequestBody Student student){
        studentMapper.insert(student);
        return AjaxResult.success(student);
    }
    @GetMapping("/{id}")
    public AjaxResult getStudentById(@PathVariable Integer id){
        Student student = studentMapper.selectById(id);
        return AjaxResult.success(student);
    }
    @PutMapping
    public AjaxResult updateStudent(@RequestBody Student student){
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
        return AjaxResult.success(student);
    }

    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public AjaxResult updateStudentViolation(@PathVariable Integer id,@PathVariable Integer score){
        Student student = studentMapper.selectById(id);
        student.setViolationCount((short)(student.getViolationCount() + 1));
        student.setViolationScore((short)(student.getViolationScore() + score));
        studentMapper.updateById(student);
        return AjaxResult.success();
    }
}
